#include <stdio.h>
#include "./common.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

/****
 * 按位输出二进制
 */ 
void xprint(unsigned char *tdata, unsigned int dlen, unsigned short rlen) {
    size_t i = 0;
    for(i = 0; i < dlen; i++) {
        printf("%02x ", tdata[i]);
        if((i+1) % rlen == 0 && i != (dlen-1)) {
            printf("\n");
        }
    }
    printf("\n");
}

int main(int argc, char const *argv[])
{
    printf("---------测试解析网络数据 \n");
    struct GUIDE_TO_MONITOR *cdt;
    
    unsigned char buff[1024];
    unsigned char *ptr = buff;
    int fd = open("/mnt/cephfs/2022/05/17/6666/091050/6666_07_25_20220517_091050747040.dat", O_RDWR);
    if(fd < 0) {
        printf("ERR: error to open file\n");
        return ;
    }
    int ret = read(fd, ptr, 1024);
    if(ret < 0) {
        printf("ERR: error to read file\n");
        return ;
    }

    // xprint(ptr, 1024, 16);

    cdt = (struct GUIDE_TO_MONITOR *) ptr;
    PACK_HEAD *phead = NULL;
    INFO_HEAD *ihead = NULL;
    struct ST_CMD_4256 *cmd_4256 = NULL;
    int count = 0;
    while(count++ < 4) {
        phead = &cdt->PackHead;
        ihead = &cdt->InfoHead;
        cmd_4256 = &cdt->GuideData;
        printf("---> %p %p %p %p\n", cdt, phead, ihead, cmd_4256);
        // printf("-----------------phead\n");
        // printf("--> %d \n", phead->len);
        // printf("--> %d \n", phead->isAck);
        // printf("--> %d \n", phead->needFeedback);
        // printf("--> %d \n", phead->isFeedback);
        // printf("--> %d \n", phead->resendNum);
        // printf("--> %d \n", phead->bak);
        // printf("-----------------ihead\n");
        printf("--> 0x%0x \n", ihead->infoId);
        // printf("--> %d \n", ihead->infoLen);
        // printf("--> %d \n", ihead->infoTime);
        // printf("-----------------4256\n");
        printf("--> %d \n", cmd_4256->usCmdNo);
        // printf("--> %d \n", cmd_4256->usCmdAttribute);
        printf("\n");
        cdt++;
    }
    return 0;
}

